﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineLibrary.Linq;
using OnlineLibrary.ViewModels;
using OnlineLibrary.Models;

namespace OnlineLibrary.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        OnlineLibrary.Linq.OnlineLibraryDBDataContext user = new OnlineLibrary.Linq.OnlineLibraryDBDataContext();
        
        public ActionResult Index()
        {

            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                IEnumerable<OnlineLibrary.Linq.tblUser> list = user.tblUsers.ToList();
                return View(list);
            }
            else {
                return Redirect("Shared/Error");
            } 
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            OnlineLibrary.Linq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
            ViewData["UserName"] = UserView.Name;
            return View(UserView);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]

        public ActionResult Create(OnlineLibrary.Linq.tblUser collection)
        {
            try
            {
                // TODO: Add insert logic here
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true) || (Boolean.Parse(Session["Guest"].ToString()) == true))
                {
                    var exist_user = from e_user in user.tblUsers where e_user.UserName == collection.UserName select e_user;
                    if (exist_user.Count() != 0)
                    {
                        ViewData["error"] = "Username has been existed";
                        return View();
                    }
                    else if (collection.PassWord.ToString().Length < 5)
                    {
                        ViewData["error"] = "Password is too short";
                        return View();
                    }
                    collection.Role = 7;
                    user.tblUsers.InsertOnSubmit(collection);
                    user.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                } 
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /User/Edit/5
        public static IEnumerable<tblRole> GetAllRoles()
        {
            var getResult = from role in DataProvider.Db.tblRoles
                            select role;

            return getResult;
        }
        public ActionResult Edit(int id)
        {
            OnlineLibrary.Linq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
            ViewData["DDLRoles"] = new SelectList(GetAllRoles().ToList(), "RoleId", "RoleName");

            return View(UserView);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, OnlineLibrary.Linq.tblUser collection,FormCollection form)
        {
            try
            {
               using (OnlineLibraryDBDataContext onlineContext=  new OnlineLibraryDBDataContext())
               {
                    // TODO: Add update logic here
                    var list = onlineContext.tblUsers.SingleOrDefault(p => p.UserId == id);
                    list.UserId = collection.UserId;
                    list.Name = collection.Name;
                    list.UserName = collection.UserName;
                    list.PassWord = collection.PassWord;
                    list.Address = collection.Address;
                    if ((Boolean.Parse(Session["Admin"].ToString()) == true))
                    {
                        list.Role = int.Parse(form["DDLRoles"]);
                        onlineContext.SubmitChanges();
                    }
                    else
                    { onlineContext.SubmitChanges(); }
                
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                OnlineLibrary.Linq.tblUser UserView = user.tblUsers.Single(p => p.UserId == id);
                if (UserView.Available == true)
                {
                    ViewData["Warning"] = "Are yousure you want to delete this user " + UserView.UserName + " ?";
                }
                else { 
                    ViewData["Warning"] = "You cann't delete this user";
                }
                return View(UserView);
            }
            else
            {
                return Redirect("Shared/Error");
            } 
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
                {
                    // TODO: Add delete logic here
                    var list = user.tblUsers.Single(p => p.UserId == id);
                    user.tblUsers.DeleteOnSubmit(list);
                    user.SubmitChanges();
                    return RedirectToAction("Index");
                 }
                else {
                return Redirect("Shared/Error");
                } 
            }
            catch
            {
                return View();
            }
        }
    }
}
